import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import {authGuard, recordRouter, titleGuard} from './guards'
import {parse as qsParse,stringify as qsStringify} from 'qs'
import { AuthRole } from '@/headers/admin/auth.h'
import { AxiosError } from 'axios'
import { RespMessage } from '@/headers/admin/cons.h'

export enum RouterName {
  Home='Home',
  Login='Login',
  Dashboard='Dashboard',
  OverView='OverView',
  Error='Error',
  ErrorAny='ErrorAny',
  ClubManage='ClubManage',
  ClubCreate='ClubCreate',
  ClubEdit='ClubEdit',
  ClubList='ClubList',
  ClubPageCreate='ClubPageCreate',
  AssetsManage='AssetsManage',
  AssetsUpload='AssetsUpload',
  AssetsList='AssetsList',
  AssetsEdit='AssetsEdit',
  UserManage = 'UserManage',
  UserCreate='UserCreate',
  UserEdit='UserEdit',
  UserList='UserList'
}
const adminAndLeaderAuth = AuthRole.Admin|AuthRole.Leader;
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: RouterName.Home,
    meta:{
      title:"首页",
      auth:AuthRole.Guest
    },
    component: ()=> import('../views/admin/HomePage.vue')
  },
  {
    path:"/login",
    name:RouterName.Login,
    meta:{
      title:"登录",
      auth:AuthRole.Guest
    },
    component: ()=> import('../views/admin/LoginPage.vue')
  },
  {
    path:"/dashboard",
    name:RouterName.Dashboard,
    meta:{
      title:"管理面板",
      auth:adminAndLeaderAuth
    },
    redirect:{
      name:RouterName.OverView
    },
    component:()=> import('../views/admin/DashboardPage.vue'),
    children:[
      {
        path:'overview',
        name:RouterName.OverView,
        meta:{
          title:"总览",
          auth:adminAndLeaderAuth,
        },
        component:()=>import('../views/admin/OverViewPage.vue')
      },
      {
        path:"club",
        name:RouterName.ClubManage,
        meta:{
          title:"社团管理",
          auth:adminAndLeaderAuth,
          isGroup:true
        },
        component:()=> import('../views/admin/ClubManagePage.vue'),
        children:[
          {
            path:"create",
            name:RouterName.ClubCreate,
            meta:{
              title:"社团创建",
              auth:AuthRole.Admin
            },
            component:()=> import('../views/admin/club/ClubCreatePage.vue')
          },
          {
            path:"list",
            name:RouterName.ClubList,
            meta:{
              title:"社团列表",
              auth:AuthRole.Admin
            },
            component:()=> import('../views/admin/club/ClubListPage.vue')
          },
          {
            path:"edit",
            name:RouterName.ClubEdit,
            meta:{
              title:"社团编辑",
              auth:adminAndLeaderAuth
            },
            component:()=>import('../views/admin/club/ClubEditPage.vue')
          },
          {
            path:"page-create",
            name:RouterName.ClubPageCreate,
            meta:{
              title:"社团页面创建",
              auth:adminAndLeaderAuth
            },
            component:()=> import('../views/admin/club/ClubPageCreatePage.vue')
          }
        ]
      },
      {
        path:"assets",
        name:RouterName.AssetsManage,
        meta:{
          title:"资源管理",
          auth:adminAndLeaderAuth,
          isGroup:true
        },
        component:()=> import('../views/admin/AssetsManagePage.vue'),
        children:[
          {
            path:"upload",
            name:RouterName.AssetsUpload,
            meta:{
              title:"资源上传",
              auth:adminAndLeaderAuth
            },
            component:()=> import('../views/admin/assets/AssetsUploadPage.vue')
          },
          {
            path:"list",
            name:RouterName.AssetsList,
            meta:{
              title:"资源列表",
              auth:adminAndLeaderAuth
            },
            component:()=> import('../views/admin/assets/AssetsListPage.vue')
          }
        ]

      },
      {
        path:"user",
        name:RouterName.UserManage,
        meta:{
          title:"用户管理",
          auth:AuthRole.Admin,
          isGroup:true
        },
        component:()=> import('../views/admin/UserManagePage.vue'),
        children:[
          {
            path:"create",
            name:RouterName.UserCreate,
            meta:{
              title:"用户创建",
              auth:AuthRole.Admin
            },
            component:()=> import('../views/admin/user/UserCreatePage.vue')
          },
          {
            path:"edit",
            name:RouterName.UserEdit,
            meta:{
              title:"用户编辑",
              auth:AuthRole.Admin
            },
            component:()=> import('../views/admin/user/UserEditPage.vue')
          },
          {
            path:"list",
            name:RouterName.UserList,
            meta:{
              title:"用户列表",
              auth:AuthRole.Admin
            },
            component:()=> import('../views/admin/user/UserListPage.vue')
          }
        ]
      },
    ]
  },
  {
    path: '/about',
    name: 'About',
    meta:{
      title:"关于"
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path:"/error",
    name:RouterName.Error,
    meta:{
      title:"发生错误了"
    },
    component:()=> import('../views/admin/ErrorPage.vue')
  },
  {
    path:"/:any*",
    name:RouterName.ErrorAny,
    meta:{
      title: "发生错误了"
    },
    component: ()=> import('../views/admin/ErrorPage.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
  scrollBehavior(_from,_to,_pos){
    return false
  },
  parseQuery(search){
    return qsParse(search) as any
  },
  stringifyQuery(query) {
    return qsStringify(query)
  }
})
router.beforeEach(recordRouter())


router.beforeEach(titleGuard("微光社团后台-"));
router.beforeEach(authGuard())




router.onError((e)=>{
  console.error(e);
  if(e.response) {
    const axiosError:AxiosError = e;
    if(axiosError.response?.data?.error === RespMessage.RequireLogin) {

      if(router.options.to) {
        router.push({name:"Login",query:{
          redirect:router.options.to.fullPath
        }})
        return;
      }
    }
  }
  router.push({name:"Error",params:e}).then();

})
export default router
export const routerTree = routes;
